package com.ruoyi.web.controller.system;

import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.system.domain.PostComment;
import com.ruoyi.system.domain.vo.PostCommentAuditVo;
import com.ruoyi.system.service.PostsCommentService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 动态评论控制器
 *
 * 说明：当前实现为接口打通示例，返回结构化数据供前端联调展示；
 * 后续可替换为持久化实现（建表、Mapper、Service）
 */
@RestController
@RequestMapping("/system/posts/comments")
@Api("动态评论")
public class PostsCommentsController extends BaseController {
    /** 评论业务服务 */
    @Autowired
    private PostsCommentService commentService;
    /** 评论列表（分页） */
    @ApiOperation("评论列表")
    @PreAuthorize("@ss.hasPermi('system:posts:list')")
    @GetMapping("/list")
    public TableDataInfo list(Long postId, Integer pageNum, Integer pageSize) {
        startPage();
        PostComment query = new PostComment();
        query.setPostId(postId);
        List<PostComment> list = commentService.selectList(query);
        return getDataTable(list);
    }

    /** 获取评论详情 */
    @ApiOperation("评论详情")
    @PreAuthorize("@ss.hasPermi('system:posts:query')")
    @GetMapping("/{id}")
    public AjaxResult get(@PathVariable("id") Long id) {
        PostComment query = new PostComment();
        query.setId(id);
        List<PostComment> list = commentService.selectList(query);
        return AjaxResult.success(list != null && list.size() > 0 ? list.get(0) : null);
    }

    /** 删除单条评论 */
    @ApiOperation("删除评论")
    @PreAuthorize("@ss.hasPermi('system:posts:remove')")
    @DeleteMapping("/{id}")
    public AjaxResult delete(@PathVariable("id") Long id) {
        return toAjax(commentService.deleteById(id));
    }

    /** 批量删除评论 */
    @ApiOperation("批量删除评论")
    @PreAuthorize("@ss.hasPermi('system:posts:remove')")
    @DeleteMapping("/batch")
    public AjaxResult deleteBatch(@RequestBody Long[] ids) {
        return toAjax(commentService.deleteByIds(ids));
    }

    /** 审核评论（通过/违规） */
    @ApiOperation("审核评论")
    @PreAuthorize("@ss.hasPermi('system:posts:edit')")
    @PutMapping("/audit")
    public AjaxResult audit(@Validated @RequestBody PostCommentAuditVo vo) {
        return toAjax(commentService.audit(vo));
    }

    /** 获取评论树（嵌套回复） */
    @ApiOperation("评论树")
    @PreAuthorize("@ss.hasPermi('system:posts:list')")
    @GetMapping("/tree/{postId}")
    public AjaxResult tree(@PathVariable("postId") Long postId) {
        return AjaxResult.success(commentService.getCommentTree(postId));
    }
}
